$(function () {
    initArtCateList()

    function initArtCateList() {
        axios.get('/my/article/cates').then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            let arr = []
            res.data.forEach(ele => {
                if (ele.Id < 0) return
                arr.push(`
                        <tr>
                        <td>${ele.Id}</td>
                        <td>${ele.name}</td>
                        <td>${ele.alias}</td>
                        <td>
                            <button data-id="${ele.Id}" class ="layui-btn btn-edit layui-btn-xs"> 编辑 </button>
                            <button data-id="${ele.Id}" class="del-edit layui-btn layui-btn-danger layui-btn-xs">删除</button>
                        </td>
                        </tr>
            `)
            });
            $('tbody').empty().html(arr.join(""))
        })
    }
    let indexAdd
    //添加文章弹出层
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '250px'],
            content: `
                <form id="addForm" class="layui-form" action="">
                    <!-- 分类名称 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <!-- 分类别名 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <!-- 按钮 -->
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn " lay-submit lay-filter="formDemo">确认添加</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>`
        });
    })
    //添加文章分类
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        axios.post('/my/article/addcates', $(this).serialize()).then(({
            data: res
        }) => {
            console.log(res);
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            layui.layer.msg('恭喜您添加成功')
            //刷新页面
            initArtCateList()
            //关闭对话框
            layui.layer.close(indexAdd)
        })
    })
    //修改文章分类
    let indexEdit
    $('tbody').on('click', '.btn-edit', function () {
        //点击编辑按钮做两件事  弹出层
        indexEdit = layer.open({
            type: 1,
            title: '修改文章类别',
            area: ['500px', '250px'],
            content: `
            <form id = "editForm" lay-filter="editForm" class="layui-form" action= "" >
                <!-- 隐藏域，填充Id值 将来修改数据的时候使用 -->
                <input type="hidden" name="Id">
                    <!-- 分类名称 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <!-- 分类别名 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <!-- 按钮 -->
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button  class="layui-btn" lay-ubmit lay-filter="formDemo">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>`
        });
        //点击编辑按钮做两件事  赋值
        //获取数据和渲染数据，要写到.btn-edit的点击事件里面
        let id = $(this).attr('data-id')
        //：id 动态参数（路由参数）
        //特点：不能省略   ：一定要去掉   id是动态变化的
        axios({
            method: 'GET',
            url: '/my/article/cates/' + id
        }).then(({
            data: res
        }) => {
            console.log(res);
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            layui.form.val("editForm", res.data)
        })
    })
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        axios.post('/my/article/updatecate', $(this).serialize()).then(({
            data: res
        }) => {
            console.log(res);
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            layui.layer.msg('恭喜您修改成功')
            //刷新页面
            initArtCateList()
            //关闭对话框
            layui.layer.close(indexEdit)
        })
    })
    //删除
    $('tbody').on('click', '.del-edit', function () {
        //获取自定义属性
        let id = $(this).attr('data-id')
        layer.confirm('确认删除吗?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //发送ajax
            axios({
                url: '/my/article/deletecate/' + id
            }).then(({
                data: res
            }) => {
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                layer.msg('恭喜您，删除文章类别成功')
                initArtCateList()
            })
            //关闭询问框
            layer.close(index)
        })
    })
})